﻿using System;

namespace TrinityAPI.Base.Models
{
    /// <summary>
    /// 异常基类
    /// </summary>
    public class BaseException : Exception
    {
        private string _code;

        public string Code
        {
            get { return _code; }
        }

        private object[] _formatkeys;

        public object[] FormatKeys
        {
            get { return _formatkeys; }
        }

        public override string Message
        {
            get
            {
                if (_formatkeys != null && _formatkeys.Length > 0 && !string.IsNullOrWhiteSpace(base.Message))
                {
                    try
                    {
                        return string.Format(base.Message, _formatkeys);
                    }
                    catch
                    { }
                }
                return base.Message;
            }
        }

        public BaseException()
            : base("Unknown error")
        {
            this._code = ErrorCode.E000000001.ToString();
        }

        public BaseException(string code)
            : base("")
        {
            this._code = code;
        }

        public BaseException(string code, string message)
            : base(message)
        {
            this._code = code;
        }

        public BaseException(string code, string message, params object[] args)
            : base(message)
        {
            this._code = code;
            this._formatkeys = args;
        }
    }
}
